import csv

# 读取CSV文件
def read_consumption_data(file_path):
    with open(file_path, 'r', encoding='gbk') as f:
        reader = csv.reader(f)
        headers = next(reader)  # 读取表头
        data = {row[0]: row[1:] for row in reader}  # 省份作为键，消费数据作为值
    return headers, data

# 主程序
def main():
    file_path = "python-project/static/xiaofei.csv"
    headers, data = read_consumption_data(file_path)
    
    print("可查询的省份有：")
    print(", ".join(data.keys()))
    
    print("\n可查询的项目有：")
    print(", ".join(headers[1:]))  # 跳过第一个"地区"列
    
    province = input("\n请输入要查询的省份：")
    items = input("请输入要查询的项目(多个项目用逗号分隔)：").split('，')
    items = [item.strip() for item in items]  # 去除前后空格
    
    try:
        print(f"\n{province}的消费数据：")
        for item in items:
            item_index = headers.index(item)
            result = data[province][item_index - 1]
            print(f"{item}: {result}元")
    except ValueError:
        print("错误：输入的某个项目不存在")
    except KeyError:
        print("错误：输入的省份不存在")

if __name__ == "__main__":
    main()